package com.xy.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.model.Admin;
import com.xy.model.StockCheck;
import com.xy.model.StockSelect;
import com.xy.service.impl.StockCheckServiceImpl;
import com.xy.service.impl.StockServiceImpl;

/**
 * Servlet implementation class StockCheckServlet
 */
@WebServlet("/stockCheckServlet")
public class StockCheckServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StockCheckServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		if ("add".equals(request.getParameter("status"))) {
			String[] s = request.getParameterValues("checkbox");
			int count = Integer.valueOf(request.getParameter("count"));
			String remark = request.getParameter("remark");
			// List<StockSelect> ss = new ArrayList();
			for (int i = 0; i < s.length; i++) {
				StockCheck sc = new StockCheck();
				StockSelect stock = (StockSelect) new StockServiceImpl().get(Integer.valueOf(s[i]));
				// ss.add(stock);
				sc.setCount(Math.abs(count));
				sc.setAdmin((Admin) request.getSession().getAttribute("admin"));
				sc.setCell(stock.getCell());
				sc.setDateTime(new Date());
				sc.setRemark(remark);
				sc.setTextBook(stock.getTextBook());
				if (count < 0) {
					sc.setStatus("-1");
					stock.setAmount(stock.getAmount() - Math.abs(count));
				} else {
					sc.setStatus("1");
					stock.setAmount(stock.getAmount() + Math.abs(count));
				}
				new StockCheckServiceImpl().add(sc);
				new StockServiceImpl().update(stock);
				request.getRequestDispatcher("/pages/public/main.jsp").forward(request, response);
			}

		}else if ("query".equals(request.getParameter("status"))) {
			int currentPage = 0;
			if (request.getParameter("goalpage") == null) {
				currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
			} else
				currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

			String admin = null;
			String start = null;
			String end = null;
			String list = null;

			if (request.getParameter("list") == null) {
				start = request.getParameter("start") == null ? "%" : request.getParameter("start");
				end = request.getParameter("end") == null ? "%" : request.getParameter("end");
				admin = request.getParameter("admin") == null ? "%" : request.getParameter("admin");
				list = start + "," + end + "," + admin;
			} else {
				String str = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
				String[] strs = str.split(",");
				for (int i = 0; i < strs.length; i++) {
					if (i == 0)
						start = i == 0 ? strs[i] : "%";
					if (i == 1)
						end = i == 1 ? strs[1] : "%";
					if (i == 2)
						admin = i == 2 ? strs[2] : "%";
				}
				list = start + "," + end + "," + admin;
			}

			PageResults pageResults = new StockCheckServiceImpl().query(start, end, admin, currentPage);

			request.setAttribute("pageResults", pageResults);
			request.setAttribute("status", "query");
			request.setAttribute("list", list);

			request.getRequestDispatcher("/pages/storage/storage004/get_query_pandian.jsp").forward(request, response);
		}

	}

}
